package Game;

import java.awt.Graphics2D;
import java.util.logging.*;
import javax.sound.sampled.AudioFormat;

import sound.MidiPlayer;
import sound.SoundManager;
import input.InputManager;
import sound.MusicManager;
import util.*;

/**
    GameManager manages all parts of the game.
*/
public class GameMain extends GameCore {

    static final Logger log = Logger.getLogger("Bee");
    public static final BugTracker bugTracker=new BugTracker();

    public static void main(String[] args) {
        new GameMain().run();
    }

    // uncompressed, 44100Hz, 16-bit, mono, signed, little-endian
    private static final AudioFormat PLAYBACK_FORMAT =
        new AudioFormat(44100, 16, 1, true, false);


    private MidiPlayer midiPlayer;
    private MusicManager musicManager;
    private SoundManager soundManager,temp;
    private util.ResourceManager resourceManager;
    private InputManager inputManager;
    private GameStateManager gameStateManager;


    public void init() {
        log.setLevel(Level.INFO);

        log.info("init sound manager");
        bugTracker.writeToFile("Initializing sound manager...");
        soundManager = new SoundManager(PLAYBACK_FORMAT,8);
        temp=new SoundManager(PLAYBACK_FORMAT,8);
        bugTracker.writeToFile("    successful");

        log.info("init midi player");
        bugTracker.writeToFile("Initializing midi player...");
        midiPlayer = new MidiPlayer();
        bugTracker.writeToFile("    successful");
        
        musicManager=new MusicManager();

        log.info("init gamecore");
        bugTracker.writeToFile("Initializing gamecore...");
        super.init();
        bugTracker.writeToFile("    successful");

        log.info("init input manager");
        bugTracker.writeToFile("Initializing input manager...");
        inputManager = new InputManager(
            screen.getFullScreenWindow());
        inputManager.setCursor(InputManager.INVISIBLE_CURSOR);
        bugTracker.writeToFile("    successful");


        log.info("init resource manager");
        bugTracker.writeToFile("Initializing resource manager...");
        resourceManager = new TilegameResourceManager(
            screen.getFullScreenWindow().getGraphicsConfiguration(),
            soundManager, midiPlayer);
        bugTracker.writeToFile("    successful");


        log.info("init game states");
        bugTracker.writeToFile("Initializing game-states...");
        gameStateManager = new GameStateManager(inputManager,
            resourceManager.loadImage("loadingsplash.jpg"));
        gameStateManager.addState(new MainGameState(
            soundManager, musicManager,
            screen.getWidth(), screen.getHeight()));
        gameStateManager.addState(
            new SplashGameState("gamesplash.jpg"));
        gameStateManager.addState(new MenuGameState(screen,musicManager,temp, "clouds.jpg"));
        bugTracker.writeToFile("    successful");

        // load resources (in separate thread)
        new Thread() {
            public void run() {
                log.info("loading resources");
                bugTracker.writeToFile("loading resources...");
                gameStateManager.loadAllResources(resourceManager);
                log.info("setting to Splash state");
                gameStateManager.setState("Splash");
            }
        }.start();
    }


    /**
        Closes any resurces used by the GameManager.
    */
    public void stop() {
        log.info("stopping game");
        bugTracker.writeToFile("stopping game");
        super.stop();
        log.info("closing midi player");
        bugTracker.writeToFile("closing midi player");
        midiPlayer.close();
        log.info("closing sound manager");
        bugTracker.writeToFile("closing sound manager");
        soundManager.close();
        bugTracker.stop();
    }

    public void update(long elapsedTime) {
        if (gameStateManager.isDone()) {
            stop();
        }
        else {
            gameStateManager.update(elapsedTime);
        }
    }


    public void draw(Graphics2D g) {
        gameStateManager.draw(g);
    }



}
